package com.wondersgroup.insurance.dnuilearn.views.canvas;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/19.
 */

public class ReavlView extends View {

    int[] imgesId = {R.drawable.avft, R.drawable.box_stack,R.drawable.bubble_frame,R.drawable.bubbles,R.drawable
    .bullseye,R.drawable.circle_filled,R.drawable.circle_outline};

    int[] imgesIdActive = {R.drawable.avft_active, R.drawable.box_stack_active,R.drawable.bubble_frame_active,R.drawable.bubbles_active,R.drawable
            .bullseye_active,R.drawable.circle_filled_active,R.drawable.circle_outline_active};


    Paint paint = null;


    Bitmap bmp = null;

    Bitmap bmpActive = null;

    int widowX,windowWidth;
    public ReavlView(Context context) {
        this(context, null);
    }

    public ReavlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReavlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        Resources res = getResources();
        int width = 0;
        int height = 0;
        Bitmap[] bmps = new Bitmap[imgesId.length];
        for (int i =0,size = bmps.length; i < size; i++){
            bmps[i] = BitmapFactory.decodeResource(res,imgesId[i]);
            width += bmps[i].getWidth();
            height = Math.max(height,  bmps[i].getHeight());
        }
        windowWidth = height;

        bmp = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmp);
        float x = startX;
        for (int i =0,size = bmps.length; i < size; i++){
            canvas.drawBitmap(bmps[i],x, 0,paint);
            x += bmps[i].getWidth();
            bmps[i].recycle();
        }

        // ***********************active*******************
        for (int i =0,size = bmps.length; i < size; i++){
            bmps[i] = BitmapFactory.decodeResource(res,imgesIdActive[i]);
        }
        windowWidth = height;
        bmpActive = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
        x = 0;
        canvas = new Canvas(bmpActive);
        for (int i =0,size = bmps.length; i < size; i++){
            canvas.drawBitmap(bmps[i],x, 0,paint);
            x += bmps[i].getWidth();
            bmps[i].recycle();
        }

        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
    }

    float startX = 0;

    Rect widowRect = null;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        widowX = (getWidth() -windowWidth)/2;
        widowRect = new Rect(widowX,0,widowX+windowWidth,windowWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
//        for (int i =0,size = bmps.length; i < size; i++){
//            canvas.drawBitmap(bmps[i],x, 0,paint);
//            x += bmps[i].getWidth();
//        }

        canvas.drawBitmap(bmp,startX, 0,paint);

        canvas.clipRect(widowRect);

        canvas.drawBitmap(bmpActive,startX, 0,paint);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            startX += distanceX;
            if (startX + bmp.getWidth() < widowX + windowWidth ){
                startX = widowX + windowWidth - bmp.getWidth();
            }else if (startX >widowX ){
                startX = widowX;
            }

            invalidate();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    });
}
